#ifndef _COMMUNICATORUTIL_H_
#define _COMMUNICATORUTIL_H_


// maximum size of data read from ports (in bytes)
#define DECODER_BUFFER_SIZE 2048 //used for receiving from front end and data proxy
#define FRONTEND_BUFFER_SIZE 10485760 // 10 MB receive from decoder (could be very large integer)


#define DECODER_DATAPROXY_TIMEOUT 20 // in seconds
#define DECODER_FRONTEND_TIMEOUT 1000 // means frontend is disconnected 
#define FRONTEND_DECODER_TIMEOUT 200 // decoder is disconnected

//return status of reading and writing operations by sockets
enum e_status {SUCCESS, ERROR, TERMINATED, DISCONNECTED, TIMEOUT};

e_status connectToClient(const char* name, const char* addr, int port, int* socket);
e_status runServerAndConnect(const char* name, int port, int* serverSocket, int* clientSocket);
e_status terminateSocket(int sock);
e_status  readChar(int sock, char t);

/*
   Params:
      fd       -  (int) socket file descriptor
      buffer - (char*) buffer to hold data
      len     - (int) maximum number of bytes to recv()
      flags   - (int) flags (as the fourth param to recv() )
      to       - (int) timeout in milliseconds
   Results:
      int      - The same as recv, but -2 == TIMEOUT
   Notes:
      You can only use it on file descriptors that are sockets!
      'to' must be different to 0
      'buffer' must not be NULL and must point to enough memory to hold at least
 'len' bytes
*/
        int recv_to(int fd, char *buffer, int len, int flags, int to);

#endif
